#ifndef _MB_HCI_H_
#define _MB_HCI_H_

#include "mb_type.h"

/* 用户指令解析表 */
#define HCI_REQ_OR_MARK           '|'
#define HCI_REQ_A2I(x)            (x - '0')
/* 请求在一个数值中的bit域: 1111 111 1 */
#define HCI_1ST_REQ_BIT_DOMAIN(x)       ((HCI_REQ_A2I(x)) << 4)
#define HCI_2ND_REQ_BIT_DOMAIN(x)       ((HCI_REQ_A2I(x)) << 1)
#define HCI_3RD_REQ_BIT_DOMAIN(x)       ((HCI_REQ_A2I(x)) << 0)

/* 菜单指令级列表 */             /*1ST*/  /* 2ND */  /* 3RD */
#define HCI_1ST_REQ_SELECT_PROT   '1'
    #define HCI_2ND_REQ_PROT_XMODEM         '0'
    #define HCI_2ND_REQ_PROT_YMODEM         '1'
    #define HCI_2ND_REQ_PROT_UNKNOWN        HCI_2ND_REQ_PROT_YMODEM
#define HCI_1ST_REQ_SELECT_CMC    '2'
    #define HCI_2ND_REQ_CMC_UART            '0'
    #define HCI_2ND_REQ_CMC_NET             '1'
    #define HCI_2ND_REQ_CMC_WIFI            '2'
    #define HCI_2ND_REQ_CMC_LORA            '3'
    #define HCI_2ND_REQ_CMC_4G              '4'
    #define HCI_2ND_REQ_CMD_UNKNOWN         HCI_2ND_REQ_CMC_4G
#define HCI_1ST_REQ_SELECT_STO    '3'
    #define HCI_2ND_REQ_STO_ROM             '0'
    #define HCI_2ND_REQ_STO_SPIFLASH        '1'
    #define HCI_2ND_REQ_STO_QSPIFLASH       '2'
    #define HCI_2ND_REQ_STO_SDCARD          '3'
    #define HCI_2ND_REQ_STO_UNKNOWN         HCI_2ND_REQ_STO_SDCARD
        #define HCI_3RD_REQ_STO_ERASE                  '0'
        #define HCI_3RD_REQ_STO_DNOTERASE              '1'
#define HCI_1ST_REQ_UPDATE_FW     '4'
#define HCI_1ST_REQ_ENTRY_APP     '5'
#define HCI_1ST_REQ_RESET         '6'
#define HCI_1ST_REQ_EXIT_BOOT     '7'

/* 提示延迟等待信息 */
#define HCI_DELAY_ABORT_KEY       'w'
#define HCI_DELAY_NOTICE          "\n键入 'w' 键中断倒计时: "
#define HCI_DELAY_NOTICE_LEN      sizeof(HCI_DELAY_NOTICE)

/* 退出当前输入操作的指令 */
#define HCI_EXIT_CUR_INPUT        'q'

/* 反馈用户操作结果 */
#define HCI_RESPONSE_SUCC         "\n操作结果: 成功\n"
#define HCI_RESPONSE_FAIL         "\n操作结果: 失败\n"

#define HCI_RESPONSE_LEN          sizeof(HCI_RESPONSE_SUCC)

/* 用户交互的行为状态 */
typedef struct HCI_ACTION_STATUS_STR
{
    MB_STATUS is_start_recv;   /* 是否开始接收 */
    MB_STATUS is_respond;      /* 是否收到 用户 的回应 (输入指令) */
    MB_STATUS is_geted;        /* 指令是否被取走 */
    MB_STATUS is_delay_noticed; /* 是否显示过延时提醒 */
} hci_action_status, *hci_action_status_t;

/* HCI 私有数据 */
typedef struct HCI_PRVT_STR
{
    cmc_op *          carrier;        /* 数据通道操作 */
    check_timeout     check_timeout;
    uint8_t           tx_buf[HCI_TXBUF_MAX_LEN];       /* 数据发送缓冲区 */
    uint8_t           rx_buf[HCI_RXBUF_MAX_LEN];       /* 数据接收缓冲区 */
    hci_action_status action_status;                   /* 用户交互的行为状态 */
    hci_req           reqs;
    mb_buf_attr       req_cmd;                         /* 缓存用户输入的指令 */
    uint8_t           req_cmd_buf[8];
} hci_prvt, *hci_prvt_t;

/* 注册 prot 协议操作 */
hci_op_t mb_hci_op_register(void);

#endif
